<template>
  <div>
    <el-row>
      <el-col style="margin-top: 10px">
        <el-button
          style="float: right; width: 20%"
          type="primary"
          @click="down_load"
          >导出</el-button
        >
        <el-table
          class="box-table"
          :data="Data"
          :stripe="true"
          :border="true"
          :fit="true"
          v-if="Data!=null"
        >
          <el-table-column
            prop="userid"
            label="咨询用户ID"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="userage"
            label="用户名"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="usersex"
            label="性别"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="咨询时间"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="auto"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                size="small"
                @click="info(scope.row);dialogVisible = true"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col style="margin: 0 auto">
        <el-card>
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              :total="Total"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-col>

      <el-dialog
        v-model="dialogVisible"
        title="用户咨询问题详情"
        width="50%"
        :before-close="handleClose"
      >
          <el-table
          class="box-table"
          :data="msg_info"
          :stripe="true"
          :border="true"
          :fit="true"
        >
          <el-table-column
            prop="problem"
            label="问题"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="answer"
            label="问题答案"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
        </el-table>

      </el-dialog>
    </el-row>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, toRaw, onMounted, onBeforeMount } from "vue";
import apiService from "@/services/apiService";
import { conversionUtcDate, base64ToExcelAndDownload } from "@/utils/auth";
import { v4 as uuidv4 } from "uuid";
import { ElMessageBox } from "element-plus";

defineOptions({
  // name 作为一种规范最好必须写上并且和路由的name保持一致
  name: "Zixun"
});

//表格数据
const Data = ref<any>([]);
const currentPage = ref(1); // 当前页数
const pageSize = ref(10); // 每页条数
const Total = ref<number>(1); //总条数

//获取表单数据
const GetList = () => {
  const params = {
    msgid: "PagingFindSearchRecord",
    token: "BC65FD34-931E-6E6D-7979-EFD67A547B56",
    seq: uuidv4(),
    body: {
      pageno: currentPage.value,
      pagesize: pageSize.value,
      userid: ""
    }
  };

  apiService
    .post("", params)
    .then((res: any) => {
      // 使用 any 类型断言
      console.log(res.data);
      let arr = res.data.body.searchrecords;
      for (let i = 0; i < arr.length; i++) {
        arr[i].createtime = conversionUtcDate(arr[i].createtime, "local");
      }
      Data.value = arr;
      Total.value = res.data.body.recordnumber;
    })
    .catch(error => {
      console.error("Error fetching data:", error);
    });
};

//详情数据
const msg_info =ref<any>([])
const info = row => {
  msg_info.value =row.recorddetails
};

//导出
const down_load = () => {
  const params = {
    msgid: "ExportSearchRecord",
    token: "BC65FD34-931E-6E6D-7979-EFD67A547B56",
    seq: uuidv4(),
    body: {
      pageno: currentPage.value,
      pagesize: pageSize.value,
      userid: "",
      begintime: "",
      endtime: ""
    }
  };
  apiService.post("", params).then((res: any) => {
    base64ToExcelAndDownload(res.data.body.base64data, "艾滋病咨询记录.xlsx");
  });
};

// 监听页码变化
const handleCurrentChange = val => {
  currentPage.value = val;
  GetList();
};

// 监听每页条数变化
const handleSizeChange = val => {
  pageSize.value = val;
};

const dialogVisible = ref(false);

const handleClose = (done: () => void) => {
  // ElMessageBox.confirm("确认关闭?")
  //   .then(() => {
      done();
    // })
    // .catch(() => {
    //   // catch error
    // });
};
onBeforeMount( () => {
  // GetList();
});
onMounted( () => {
  GetList();
});
</script>

<style lang="scss" scoped></style>
